package com.swotxu.mall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swotxu.common.utils.PageUtils;
import com.swotxu.common.utils.Query;
import com.swotxu.common.utils.R;
import com.swotxu.mall.ware.dao.WareInfoDao;
import com.swotxu.mall.ware.entity.WareInfoEntity;
import com.swotxu.mall.ware.feign.MemberFeignService;
import com.swotxu.mall.ware.service.WareInfoService;
import com.swotxu.mall.ware.vo.FareVo;
import com.swotxu.mall.ware.vo.MemberAddressVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired
    MemberFeignService memberFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<WareInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        String key = (String) params.get("key");
        lambdaQueryWrapper.and(StringUtils.isNotEmpty(key), wrapper -> {
            wrapper.eq(WareInfoEntity::getId, key)
                    .or().like(WareInfoEntity::getName, key)
                    .or().like(WareInfoEntity::getAddress, key)
                    .or().like(WareInfoEntity::getAreacode, key);
        });

        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                lambdaQueryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public FareVo getFare(Long addrId) {
        FareVo fareVo = new FareVo();
        R r = memberFeignService.addrInfo(addrId);
        if (!r.isOk()) {
            return null;
        }
        MemberAddressVo data = r.getValue("memberReceiveAddress", new TypeReference<MemberAddressVo>() {
        });
        String phone = data.getPhone();
        //123
        String substring = phone.substring(phone.length() - 1);
        BigDecimal bigDecimal = new BigDecimal(substring);
        fareVo.setAddress(data);
        fareVo.setFare(bigDecimal);
        return fareVo;
    }

}